<?php
namespace app\index\controller;

use \think\Db;
use \think\Controller;
use think\facade\Request;
use \think\Url;

class Goods extends Controller
{
    public function index()
    {
        $goods = Db::table('books')->select();

		$this->assign('goods',$goods);
		return $this->fetch('index');  //index.html
	}

    public function del()
    {
		$book_id =Request::get('id');
		// var_dump($id);
        Db::table('books')->where('book_id',$book_id)->delete();
		$this->success("删除成功");
		
    }
	
    public function add()
    {
        return $this->fetch('add');
    }
	
    public function insert()
    {
		$book_name=Request::post('book_name');
		$book_author=Request::post('book_author');
		$pub_time=Request::post('pub_time');
		
		$data = ['book_name' => $book_name, 
				'book_author' => $book_author,
				'pub_time' => $pub_time,
				];
		Db::table('books')->insert($data);
        $this->success("增加成功", url('index/Goods/index'));
    }
	
    public function edit()
    {
		$id =Request::get('id');
		$info = Db::table('books')->where('book_id',$id)->find();
		$this->assign('info',$info);
		
        return $this->fetch('edit');
    }
	
    public function save()
    {
        $book_id=Request::post('book_id');
		$book_name=Request::post('book_name');
		$book_author=Request::post('book_author');
		$pub_time=Request::post('pub_time');
		
		$data = [
				'book_id' => $book_id, 
				'book_name' => $book_name, 
				'book_author' => $book_author,
				'pub_time' => $pub_time,
				];
		Db::table('books')->where('book_id', $book_id)->update($data);
        $this->success("保存成功", url('index/goods/index'));
    }
	
	public function GetGoodsFromID($id){
		
		$info = Db::table('books')->where('id',$id)->find();
		return $info;
	}
	
	
}
